<!-- Web Upload Data Delete PHP File -->
<!-- Reveions History ...
	Rev 1.00 at 2015/06/08 by Max Yin:
		1. First time create
	Rev 1.01 at 2015/07/08 by Max Yin:
		1. 由標楷體, 改成新細明體
		2. 增加 FTP 上 Delete File 功能, 及是否要 delete 勾选
		3. 可删除 MySQL 上, 不同站别, 但相同版本及描述
	Reb 1.08 at 2015/12/08 by Max Yin,
		1. Web SMT upload 增加-回流焊的选项。(eric_liang)
		2. Web SMT upload/download, 由填入栏位改为下拉选单。(eric_liang)
		3. Web Upload/Downlaod 新增加 ESD 下拉选单。(jianwei_wang)
		4. 移除 ftp IP 的栏位输入, 由 Web 程式内定 FTP 路径。(Lucas)
		5. 修正重复上传, 却只有版本不对, 导致无法上传问题。(mingshen_feng)
		6. 修正若上传者, 若没有上传者名称, 会要求重新登入讯息。
	Reb 1.09 at 2019/3/25 by guangshui_xu
		1.增加'删除记录'功能，记录每次删除操作。
-->
<html>
	<body background="pic/background_06.gif">
	<head>
		<font face="新細明體,time new roman,Arial">
		<meta charset="UTF-8">
	</head>
	<body>
		<center>
			<font size="5" color="#0000FF"><b>--- 程式,查询结果 ---</b></font><br>
			<hr>
			<?php
				date_default_timezone_set('PRC');//设置当前时间为中国时间
				require_once('class_package/common.php');
				$admin_member = new member();
				$history_username	= @$_COOKIE['source_name'];
				$partnum			= @$_COOKIE['source_pn'];
				$tmp_by				= $_COOKIE['login_user'];
				$tmp_pn				= "";
				$tmp_wsid			= "";
				$tmp_tpver			= "";
				$tmp_work_order		= "";	
				$tmp_line			= "";
				$tmp_panel			= "";
				$tmp_ftpip			= "";
				$tmp_ftpfile        = "N";
				
				if ( ! @isset($_GET[pn]) || ! @isset($_GET[wsid]) || ! @isset($_GET[tpver]) || ! @isset($_GET[creator]) || ! @isset($_GET[work_order]) || ! @isset($_GET[line]) || ! @isset($_GET[panel]) || ! @isset($_GET[ftpip]) ) {
					echo "<script language=javascript>alert('无此信息!');history.go(-1);</script>";
					exit();
				}
				// echo "hsdfkgagsfoask";
				$tmp_pn			= @$_GET[pn];
				$tmp_wsid		= @$_GET[wsid];
				$tmp_tpver		= @$_GET[tpver];
				$tmp_work_order	= @$_GET[work_order];	
				$tmp_line		= @$_GET[line];
				$tmp_panel		= @$_GET[panel];
				$tmp_ftpip		= @$_GET[ftpip];
				$creator		= @$_GET[creator];
				
				// 由 user name 去判断, 是否为 admin 成员 ...
				$is_admin_member = 0;
				foreach ( $admin_member->getMembers() as $value ) {
					if ( $tmp_by == $value ) {
						$is_admin_member = 1;
						break;
					}
				}
				
				// 非Admin成员, 不允許刪除他人建立資料的動作...
				if ( $is_admin_member == 0 ) {
					if ($tmp_by != $creator){
						echo "<script>alert('該筆資料只能由上傳者 $creator 進行刪除!');history.go(-1);</script>";
						exit();
					} 
				}
				
				// To check FTP files exist...
				if ( $tmp_ftpip == NULL ) {
					echo "<script>alert('在资料库获取不到相关该笔删除资料的FTP文件IP, 请查询后再删除...');history.go(-1);</script>";
					exit();
				}
				if ( strpos($tmp_ftpip,' ') > -1 ) {
					echo "<script>alert('输入的FTP Path Delete(FTP删除路径)的栏位中, 不能为\' \'空白字符,请用\'_\'替代后, 请重新输入!!');history.go(-1);</script>";
					exit();
				}
				
				// delete ftp old file on ftp side...
				if ( file_exists($tmp_ftpip) ) {
					if(!unlink($tmp_ftpip)){//删除ftp文件，并判断是否删除成功！			
						echo "<script>alert('ftp文件删除失败！请联系管理人员, 并查明原因...');history.go(-1);</script>";
					}
					$tmp_ftpfile        = "Y";
				}
				
				// To delete the account by user name from MySQL ...
				$result = download_delete($tmp_pn,$tmp_wsid,$tmp_tpver,$tmp_work_order,$tmp_line,$tmp_panel);				
				if ( !$result ) {
					echo "<script>alert('该程式, 删除失败! 请联系程式开发人员, 并查明原因...');history.go(-1);</script>";
					exit();
				}
				
				if (1) {
					//删除成功在这里记录：删除的人，pn,wsid， 时间，版本，工单，创建的人程式的人
					$deleteTime =date('Y-m-d H:i:s', time());
					$deleteUser=$tmp_by;	//登录/删除人		
					$re=download_deleted_record($tmp_pn,$tmp_work_order,$tmp_tpver,$tmp_wsid,$creator,$deleteUser,$deleteTime,$tmp_ftpfile);//执行插入记录
					if($re!=1){
						echo "<script>alert('该删除未被记录，但已经删除，请留存资料并联系系统管理人员！');</script>";
					}
					echo "<script>alert('该程式, 已删除成功...');history.go(-2);</script>";
					mysqli_close($conn);					// 关闭资料库
					exit();
				} else {
					// To search all table ...
					$db_select      = Sql_Download::sql_db;
					$db_table       = Sql_Download::sql_table;
					$table_name1    = Sql_Download::table_name1;	// 'webdl_pn';
					$table_name8	= Sql_Download::table_name8;	// 'webdl_create_date';
					$table_name9	= Sql_Download::table_name9;	// 'webdl_creator';
					
					// To search all table ...
					$err_chk        = '';
					$WSID_chkCnt	= 1;
					$show_num		= 0;	
					$conn			= '';
					$sql_cmd		= '';
					$rows			= 0;
					$result			= '';
					$conn = mysqli_connect(html::sql_login_host,html::sql_login_user,html::sql_login_pass,$db_select);
					if ( mysqli_connect_errno($conn) ) { 
						echo "<script>alert('MySql资料库连接失败,请联系开发人员,并查明原因...');history.go(-1);</script>";
						exit();
					}
					mysqli_set_charset($conn,'set names utf8');
					$sql_cmd = "SELECT * FROM `$db_table` WHERE `$table_name1` LIKE '%$partnum%' AND `$table_name9` LIKE '%$history_username%' ORDER BY (`$table_name8`) DESC";
					$result = mysqli_query($conn,$sql_cmd);	// 透过 execute_query 对象实例完成查询
					if ( !$result ) { 
						mysqli_close($conn);				// 关闭资料库
						echo "<script>alert('MySql资料库搜寻失败,请跟开发人员查明原因...');history.go(-1);</script>";
						exit();
					}
					
					$rows = mysqli_num_rows($result);		// 返回结果集中栏位的数量。
					if ( $rows == 0 ) { 
						echo "<script>alert('WSID(站別) 栏位中, 有某些站位查询不到, 请重新建立...');history.go(-1);</script>";
						exit();
					}
					
					echo '<br>
					<table border="2" width="75%">
						<tr bgcolor="#70DB93">
							<th>No.</th>
							<th>工單</th>
							<th>Part Number</th>
							<th>WSID</th>
							<th>TPVER</th>
							<th>线别</th>
							<th>制程别</th>
							<th>Create Date/Time</th>
							<th>Create By</th>
							<th>Description</th>
							<th>Download</th>
							<th>Delete</th>
							<th>Update</th>
						</tr>';
					
					while ( $getArray = mysqli_fetch_array($result,MYSQLI_ASSOC) ) {
						$show_num++;
						$regex = '/\/COMMON\//i';
						$put_pn          = $getArray['webdl_pn'];
						$put_wsid        = $getArray['webdl_wsid'];
						$put_tpver       = $getArray['webdl_tpver'];
						$put_work_order  = $getArray['webdl_work_order'];
						$put_line        = $getArray['webdl_line'];
						$put_panel       = $getArray['webdl_panel'];
						$put_create_date = $getArray['webdl_create_date'];
						$put_creator     = $getArray['webdl_creator'];
						$put_description = $getArray['webdl_description'];
						$put_ftpip       = $getArray['webdl_ftpip'];
						$webul_update    = "webul_download_update.html?tpver=$put_tpver,wsid=$put_wsid";
						$webul_delete    = "";
						if ( preg_match($regex, $put_ftpip) )
							$webul_delete = "webul_download_delete.html?pn=$put_pn,tpver=$put_tpver,wsid=$put_wsid";																					
						else
							$webul_delete = "webul_download_delete_c.php?pn=$put_pn&wsid=$put_wsid&tpver=$put_tpver&work_order=$put_work_order&line=$put_line&panel=$put_panel&ftpip=$put_ftpip";
						
						echo "
							<tr>
								<th>$show_num</th>
								<th><font size='4' color='#0000FF'><b>$put_work_order</b></font></th>
								<th>$put_pn</th>
								<th>$put_wsid</th>
								<th>$put_tpver</th>
								<th>$put_line</th>
								<th>$put_panel</th>
								<th>$put_create_date</th>
								<th>$put_creator</th>
								<th>$put_description</th>
								<th><a href=$put_ftpip>下 载</a></th>
								<th><a href=$webul_delete>删 除</a></th>
								<th><a href=$webul_update>修 改</a></th>
							</tr>";
					}
					echo '</table><br>';
					mysqli_free_result($result);			// 释放 MySQL $result
					mysqli_close($conn);					// 关闭资料库
				}
			?>
		</center>
	</body>
</html>

